
cap log close 
clear all
set more off 
set niceness 10
set processor 4
set scheme s1color
gl MAIN = "/Users/Wei/Dropbox"
*gl MAIN = "C:\Users\huang\Dropbox"

gl path1 = "$MAIN/uhs"
gl path2 = "$MAIN/Fertility/Workingdata"
gl path3 = "$MAIN/Fertility/Results"
gl path4 = "$MAIN/Fertility/Figures"

gl uhs_path1 = "/Users/Wei/Dropbox/uhs"
gl uhs_path2 =  "/Users/Wei/Dropbox/Wei&Peng/Workingdata" 

log using "$path3/UHS_data.log", replace 

***** Section UHS *****
use "$path1/2002p",clear 
keep hcode dcode prov pnum  a* 
gen year = 2002
save "$path2/2002_data_1",replace 
use "$path1/2002h",clear 
keep dcode hcode qs d*  e* f? f?? f??? b*
merge 1:m dcode hcode using "$path2/2002_data_1"
drop _merge 
save "$path2/2002_data",replace 
cap erase "$path2/2002_data_1.dta"

use "$path1/2003p",clear 
keep hcode dcode prov hnum  a*
gen year = 2003 
save "$path2/2003_data_1",replace 
use "$path1/2003h",clear 
keep dcode hcode qs d*  e* f? f?? f???  b*
merge 1:m dcode hcode using "$path2/2003_data_1"
drop _merge 
save "$path2/2003_data",replace 
cap erase "$path2/2003_data_1.dta"

use "$path1/2004p",clear 
keep hcode dcode prov hnum  a*

gen year = 2004
save "$path2/2004_data_1",replace 
use "$path1/2004h",clear 
keep dcode hcode qs d*  e* f? f?? f???  b*
merge 1:m dcode hcode using "$path2/2004_data_1"
drop _merge 
save "$path2/2004_data",replace 
cap erase "$path2/2004_data_1.dta"

use "$path1/2005p",clear 
keep hcode dcode prov hnum  a*

gen year = 2005
save "$path2/2005_data_1",replace 
use "$path1/2005h",clear 
keep dcode hcode qs d*  e* f? f?? f???  b*
merge 1:m dcode hcode using "$path2/2005_data_1"
drop _merge 
save "$path2/2005_data",replace 
cap erase "$path2/2005_data_1.dta"

use "$path1/2006p",clear 
keep hcode dcode prov hnum  a*

gen year = 2006 
save "$path2/2006_data_1",replace 
use "$path1/2006h",clear 
keep dcode hcode qs d*  e* f? f?? f???  b*
merge 1:m dcode hcode using "$path2/2006_data_1"
drop _merge 
save "$path2/2006_data",replace 
cap erase "$path2/2006_data_1.dta"


use "$path1/2007p",clear 
keep hcode dcode prov pnum  a*

replace a7 = (2007-int(a7/100)) 
gen year = 2007 
save "$path2/2007_data_1",replace 
use "$path1/2007h",clear 
keep dcode hcode qs d*  e* f? f?? f???  b*
merge 1:m dcode hcode using "$path2/2007_data_1"
drop _merge 
save "$path2/2007_data",replace 
cap erase "$path2/2007_data_1.dta"



use "$path1/2008p",clear 
keep hcode dcode prov pnum  a*

replace a7 = (2008-int(a7/100))
gen year = 2008 
save "$path2/2008_data_1",replace 

use "$path1/2008h",clear 
keep dcode hcode qs d*  e* f? f?? f???  b*
merge 1:m dcode hcode using "$path2/2008_data_1"
drop _merge 
save "$path2/2008_data",replace 
cap erase "$path2/2008_data_1.dta"


use "$path1/2009p",clear 
keep hcode dcode prov pnum  a*

replace a7 = (2009-int(a7/100))

gen year = 2009
save "$path2/2009_data_1",replace 

use "$path1/2009h",clear 
keep dcode hcode qs d*  e* f? f?? f???  b*
merge 1:m dcode hcode using "$path2/2009_data_1"
drop _merge 
save "$path2/2009_data",replace 
cap erase "$path2/2009_data_1.dta"



use "$path2/2002_data", clear 
append using "$path2/2003_data", nonotes
append using "$path2/2004_data", nonotes
append using "$path2/2005_data", nonotes
append using "$path2/2006_data", nonotes
append using "$path2/2007_data", nonotes
append using "$path2/2008_data", nonotes
append using "$path2/2009_data", nonotes
cap erase "$path2/2002_data.dta"
cap erase "$path2/2003_data.dta"
cap erase "$path2/2004_data.dta"
cap erase "$path2/2005_data.dta"
cap erase "$path2/2006_data.dta"
cap erase "$path2/2007_data.dta"
cap erase "$path2/2008_data.dta"
cap erase "$path2/2009_data.dta"

gen N_member= b21 
bysort dcode hcode year: egen temp_N=count(hcode)
replace N_member = temp_N if N_member==.
replace N_member = temp_N if N_member==0 

gen pcode = int(dcode/100)
replace pcode=1411 if pcode==1423
replace pcode=5001 if pcode==5002
replace pcode=5306 if pcode==5321
replace pcode=5308 if pcode==5327
replace pcode=5307 if pcode==5332
replace pcode=6209 if pcode==6221
replace pcode=6211 if pcode==6224
replace pcode=6208 if pcode==6227
replace pcode=6210 if pcode==6228
gen age = a7
g age2 = age^2/100
cap drop educ
recode a8 (1/2=3)(5/6 = 5) (7/8=6),gen(educ)

replace a12 = 99 if mi(a12) | a12 ==0 | a12 == 15
gen work_cond = a12 if year >= 2007
replace work_cond = a12 if a12 <=7 & year < 2007 
replace work_cond = 8 if a12 ==13 & year < 2007 
replace work_cond = 9 if a12 ==10 & year < 2007 
replace work_cond = 10 if a12 ==8 & year < 2007 
replace work_cond = 11 if a12 ==9 & year < 2007 
replace work_cond = 12 if a12 ==12 & year < 2007 
replace work_cond = 13 if a12 ==11 & year < 2007 
replace work_cond = 15 if a12 ==15 & year < 2007 
drop if work_cond == 8 

replace a13 = 99 if a13 == 0
replace a14 = 99 if mi(a14) | a14==0
replace a14 = 8 if a14  ==7 & year >= 2007 
replace a14 = 4 if a14 == 5 & year < 2007 
replace a14 = a14 -1 if a14 >= 5 & a14 <=7 & year < 2007 


gen occup = a14  



merge m:1 year using "$path2/cpi", nogen keep(3)
drop if a1 == 99 | a1 == 0 
drop if age > 100
gen older = age > 60 
gen young = age < 18 

gen married = a9 == 2 

gen head1 = a1 == 1 
gen head2 = a2 == 1 
gen sex = a6 

gen women = sex == 2 
gen men = sex == 1 
gen han = a5 == 1 

egen old_prop = mean(old), by(dcode hcode year)
egen young_prop = mean(young), by(dcode hcode year)
egen female_prop = mean(women), by(dcode hcode year)
egen han_p = mean(han), by(hcode dcode year)

bys year: su N_member men han age educ married head1 work_cond occup

keep if age >= 25  

gen year_birth = year - age 
*keep if year_birth <= 1979


gen lninc = ln(d92/cpi)
gen lninc_per = ln((d92/cpi)/N_member)

* Income **
gen labor_share = d921/d92 if  d921/d92<2
gen run_share = d922/d92 if  d922/d92<2
gen wealth_in_share = d923/d92 if  d923/d92<2
gen trans_in_share = d924/d92 if  d924/d92<2


gen lnexp = ln(d95/cpi)
gen lnexp_per = ln((d95/cpi)/N_member)

***** Shares in expenditure ***
gen cons_share = d951/d95 if d951/d95 <= 2
gen wealth_share = d954/d95 if d954/d95 <= 2
gen trans_out_share = d953/d95 if d953/d95 <= 2
gen so_ins_share = d955/d95 if d955/d95  <= 2
gen estate_share = d952/d95 if d952/d95  <= 2

gen lnconsump = ln(e/cpi)
gen lnconsump_per = ln((e/cpi)/N_member)
gen lnfood = ln(e1)
gen lnfood_per = ln(e1/N_member)
drop if educ == 9 | mi(educ) | educ == 0

**** Consumption shares ****
gen food_share = e1/e if e1/e<=2
gen cloth_share = f2/e if f2/e <=2
gen long_share = f3/e if f3/e <=2 
gen med_share = f4/e if f4/e <=2 
gen trans_comm_share = f5/e if f5/e <=2
gen edu_ent_share = f6/e if f6/e <=2
gen house_share = f7/e if f7/e <=2
gen other_share = f8/e if f8/e <=2
gen service_share = e0/e if e0/e <=2

**** Consumption detailed shares ****
gen grain_share = e11/e if e11/e1 <=2
gen meat_share = (e12+e17)/e if (e12+e17)/e1<=2
gen veg_share = (e13+e16)/e if (e13+e16)/e1<=2
gen sug_share = e151/e if e151/e1<=2
gen smoke_share = e152/e if e152/e1<=2
gen wine_share = e153_2/e if e153_2/e1<=2
gen drink_share = e154/e if e152/e1<=2
gen resturant_share = e19/e if e19/e1<=2

**** Long term details ***
gen durable_share = (f31+f32+f33+f34+f35)/e if (f31+f32+f33+f34+f35)/e < 2
gen housekeep_share = f36/e if f36/e < 2

**** Medical details ***
gen equi_med_share = (f41+f42)/e if  (f41+f42)/e <2 
gen drug_med_share = (f43)/e if  (f43)/e <2 
gen medicare_med_share = (f45)/e if  (f45)/e <2 

**** Transportation & Communication details ***
gen transportation_share = f51/e if f51/e<2
gen communication_share = f52/e if f52/e<2


**** Education & Entertainment details ***
gen enter_share = (f61+f62)/e if f61/e<2
gen enter_real_share = f61/e if f61/e<2
gen enter_serv_share = f62/e if f62/e<2
gen edu_share = f63/e if f63/e<2
gen edu_real_share = f631/e if f631/e<2
gen edu_serv_share = f632/e if f632/e<2
**** other details ***
gen other_real_share = f81/e if f81/e < 2 
gen other_gold_share = (f811)/e if f81/e < 2 
gen other_beauty_share = (f813+f814)/e if f81/e < 2 
gen other_serv_share = f82/e if f82/e < 2 

gen activity_share = trans_comm_share+edu_ent_share

gen saving = (d92 -e)/d92 *100
drop if mi(saving)
drop if year_birth == 0 

gen senior = educ >= 5 
gen college = educ >= 6
drop if lninc_per == . 
gen lnearning_per = ln(d921/N_member/cpi)
drop if mi(lnearning_per)

replace qs = 10 if mi(qs)
replace qs = qs/10 if year == 2009


cap drop *_food_share sug_wine_share
gen grain_food_share = grain_share/food_share
gen meat_food_share = meat_share/food_share
gen veg_food_share = veg_share/food_share
gen sug_food_share = sug_share/food_share
gen smoke_food_share = smoke_share/food_share
gen wine_food_share = wine_share/food_share
gen sug_wine_share = sug_food_share  + wine_food_share
gen drink_food_share = (drink_share)/food_share
gen resturant_food_share = resturant_share/food_share

gen house_own = b24
order prov year year_birth age  han head* qs N_member

drop  b013-temp_N


ren year yr_t
qui:{
forvalues age = 0(1)30{
gen year = year_birth + `age' 
merge m:1 year prov using "$path2/fines_use",keepusing(fine) nogen
replace fine = 0 if fine == . & year < 1979 
ren fine fine_age`age'
drop year
}
}
egen fine_birth = rowmean(fine_age0)
egen fine_1_9 = rowmean(fine_age1-fine_age9)
egen fine_10_19 = rowmean(fine_age10-fine_age19)
egen fine_20_25 = rowmean(fine_age20-fine_age25)

*egen fine_f2_5 = rowmean(fine_f2 fine_f1 fine_age0 fine_age1-fine_age5)
egen fine_6_10 = rowmean(fine_age6-fine_age10)
egen fine_11_15 = rowmean(fine_age11-fine_age15)
egen fine_16_20 = rowmean(fine_age16-fine_age20)
egen fine_21_25 = rowmean(fine_age21-fine_age25)

egen fine_6_12 = rowmean(fine_age6-fine_age12)
egen fine_13_19 = rowmean(fine_age13-fine_age19)

egen fine_6_15 = rowmean(fine_age6-fine_age15)
egen fine_16_25 = rowmean(fine_age16-fine_age25)


drop if mi(fine_10_19)
ren yr_t year 

drop if educ == 0 


compress
save "$path2/ocp_uhs_reg",replace 

log close 
